<?php
declare (strict_types = 1);

namespace app\middleware\admin;
use think\facade\Session;
use think\response\Redirect;

/***
 * Class LoginAuth
 * @package app\middleware\admin
 * @Author Vee7
 * @Date 2021/12/16
 * @Time 14:12
 * @Description 后台登录验证中间件
 */
class LoginAuth
{
    public function handle($request, \Closure $next)
    {
        $rootData = Session::get('RootData');
        //如果session无数据且当前请求地址不为login和captcha，则跳转到登录页，否则允许访问
        if(empty($rootData) && !preg_match("/login/",$request->pathinfo()) && !preg_match("/captcha/",$request->pathinfo())){
            return redirect('/admin/login');
        }
        if($rootData){
            $request->rootData = $rootData;
        }
        return $next($request);
    }
}
